package com.dexstudio.xmlparser;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.dexstudio.exceptions.EmptyXMLException;

import android.util.Log;

public class XMLParser {
	private Document doc = null;
    private Element root = null;
    
    public XMLParser(String xmlString) throws ParserConfigurationException, SAXException, IOException, EmptyXMLException {
    	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            
    	dbf.setNamespaceAware(true);
    	dbf.setIgnoringElementContentWhitespace(true);
        
        DocumentBuilder db = dbf.newDocumentBuilder();
        doc = db.parse(new InputSource(new StringReader(xmlString)));
            
        if (doc == null) {
        	Log.e("XMLParser", "Cannot parse DOC");
        	throw new EmptyXMLException();
        }
        root = doc.getDocumentElement();
    }
    
    public Document getDoc() {
    	return doc;
    }


    public Element getRoot() {
    	return root;
    }
}
